<template>
  <div class="message-item-container">
    <i class="message-unread-marker" v-if="message.status === 0"></i>
    <div class="message-item-header">
      <div
        class="message-item-title"
        :class="{ 'message-unread': message.status === 0 }"
      >
        {{ message.messageTitle }}
      </div>
      <div
        class="message-item-mark"
        v-if="message.status === 0"
        @click.prevent.stop="markReadMessage"
      >
        标记已读
      </div>
    </div>
    <div class="message-item-info">
      <span class="message-item-type">{{ message.messageType }}</span>
      <!-- <el-tag type="info">{{ message.messageType }}</el-tag> -->
      <span class="message-item-time">
        {{ message.createTime | formatDate }}
      </span>
    </div>
  </div>
</template>

<script>
import { markReadMessage } from "@/api/message"

export default {
  name: "Message",
  props: {
    message: {
      type: Object,
      default: () => {},
    },
  },
  components: {},
  filters: {
    formatDate(date) {
      var Y = date.getFullYear() + "-"
      var M = addZero(date.getMonth() + 1) + "-"
      var D = addZero(date.getDate()) + " "
      var h = addZero(date.getHours()) + ":"
      var m = addZero(date.getMinutes()) + ":"
      var s = addZero(date.getSeconds())
      return Y + M + D + h + m + s
    },
  },
  data() {
    return {
      // message: {
      //   id: "123",
      //   messageTitle:
      //     "1111111111111111111111111111111111111111111111111111111111111111123",
      //   messageType: "认证消息",
      //   status: Math.round(Math.random()),
      //   createTime: new Date(),
      // },
    }
  },
  methods: {
    async markReadMessage() {
      const res = await markReadMessage([this.message.id])
      this.$emit("markReadMessage", this.message)
    },
  },
}

// 补全0
function addZero(num) {
  return num < 10 ? "0" + num : num
}
</script>

<style lang="scss" scoped>
.message-item-container {
  width: 100%;
  padding: 10px 30px;
  transition: background 0.3s ease-in-out;
  position: relative;
  border-bottom: 1px solid #ebeef5;
  cursor: pointer;
  line-height: 20px;
  .message-unread-marker {
    position: absolute;
    left: 0;
    top: 0;
    bottom: 0;
    width: 4px;
    background: #1890ff;
    // display: none;
  }

  .message-item-header {
    display: flex;
    justify-content: space-between;
    margin-bottom: 10px;
    .message-item-title {
      color: #333;
      line-height: 20px;
      flex: 1 1 auto;
      min-width: 20px;
      text-overflow: ellipsis;
      overflow: hidden;
      white-space: nowrap;
    }
    .message-unread {
      color: #000;
      font-weight: 600;
    }
    .message-item-mark {
      display: none;
      color: #1890ff;
      flex-grow: 0;
      flex-shrink: 0;
      white-space: nowrap;
      cursor: pointer;
      // opacity: 0;
      // visibility: hidden;
      // transition: opacity 0.3s ease-in-out;
    }
  }

  .message-item-info {
    display: flex;
    justify-content: space-between;
    .message-item-type {
      color: #333;
      // flex: 1 1 auto;
      min-width: 20px;
      font-size: 12px;

      text-overflow: ellipsis;
      overflow: hidden;
      white-space: nowrap;
      padding: 0 7px;
      background-color: #f3f4f7;
      border: 1px solid #f3f4f7;
      vertical-align: middle;
    }
    .message-item-time {
      color: rgba(0, 0, 0, 0.4);
      font-size: 12px;
      flex-grow: 0;
      flex-shrink: 0;
      white-space: nowrap;
    }
  }

  &:hover {
    background: #e7eaef;
    .message-item-header {
      .message-item-mark {
        display: inline-block;
        // opacity: 1;
        // visibility: visible;
      }
    }
  }
}
</style>
